package logic

import (
	"context"
	"fmt"
	"strconv"
	"usershop/user_srv/internal/svc"
	"usershop/user_srv/model/mysql"
	"usershop/user_srv/model/redis"
	"usershop/user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateByMobileLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdateByMobileLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateByMobileLogic {
	return &UpdateByMobileLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdateByMobileLogic) UpdateByMobile(in *user.UpdatePasswordMobileRequest) (*user.UpdatePasswordMobileResponse, error) {
	// todo: 修改密码 By 手机号
	//获取短信验证码
	code := redis.GetSmsCode(in.Mobile, "pwd")
	if code == "" {
		logx.Error("缓存验证码为空")
		return nil, nil
	}
	fmt.Print("换成周的验证码......................" + code)

	u := mysql.User{}

	//手机号查询用户信息
	err := u.MobileLog(in.Mobile)
	if err != nil {
		logx.Error("手机号查询用户信息失败")
		return nil, err
	}

	fmt.Printf("修改的id是...................." + strconv.Itoa(int(u.ID)))

	sale := u.Sale

	fmt.Print("获取的盐............." + sale)

	err = u.UpdateByMobilePwd(in.UserID, in.NewPassword, sale)
	if err != nil {
		logx.Error("修改失败")
		return nil, err
	}
	return &user.UpdatePasswordMobileResponse{
		Code:   code,
		Sale:   sale,
		UserID: strconv.Itoa(int(u.ID)),
	}, nil
}
